"""
下拉框操作
    在遇到下拉框的时候，webdriver提供了一个类，Select类来操作下拉框
"""
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select

path = Service(executable_path=r'C:\Users\Laptop\Desktop\thirty_one\chromedriver.exe')
driver = webdriver.Chrome(service=path)
driver.maximize_window()
driver.implicitly_wait(10)
driver.get('https://sahitest.com/demo/selectTest.htm')

s1 = driver.find_element(By.ID,'s1Id') # 获取某个下拉框的元素

select1 = Select(s1) # 实例化Select类

# select1.select_by_index(3) # 通过索引选择第四个元素
# select1.select_by_value('o3') # 根据value，选择第四个元素
select1.select_by_visible_text('o3') # 根据文本，选择第四个元素
driver.get_screenshot_as_file('选择o3.png')

for i in select1.options: # 打印出所有选项
    print(i)

print(select1.first_selected_option) # 打印出当前选择的选项